package com.yc.web.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oyj.dao.DBHelper;
@WebServlet("/addr")
public class AddrServlet extends BasicServlet {
	private DBHelper db = new DBHelper();
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/plain;charset=utf-8");
		
		//得到op
		String op = req.getParameter("op");
		
		if("findByMno".equals(op)){
			findByMno(req,resp);
		}else if ("addAddr".equals(op)){
			addAddr(req,resp);
		}else if("delAddr".equals(op)){
			delAddr(req,resp);
		}else if("changeAddr".equals(op)){
			changeAddr(req,resp);
		}else if("currentAddr".equals(op)){
			currentAddr(req,resp);
		}
	}
	private void currentAddr(HttpServletRequest req, HttpServletResponse resp) {
		Map<String,Object> user = (Map<String, Object>) req.getSession().getAttribute("loginUser");
		int mno = Integer.parseInt(user.get("mno") + "");
		String ano = req.getParameter("ano");
		String sql_1 = "update addrinfo set flag = 0 where mno = ?";
		db.doUpdate(sql_1, mno);
		String sql = "update addrinfo set flag = 1 where ano = ?";
		int result = db.doUpdate(sql, ano);
		if(result > 0){
			this.send(resp, 1);
		}
	}
	private void changeAddr(HttpServletRequest req, HttpServletResponse resp) {
		String name = req.getParameter("name");
		String tel = req.getParameter("tel");
		String prov = req.getParameter("prov");
		String city = req.getParameter("city");
		String area = req.getParameter("area");
		String addr = req.getParameter("addr");
		String ano = req.getParameter("anos");
		
		String sql = "update addrinfo set name = ?,tel =?,province = ?,city = ?,area = ?, addr = ? where ano = ?";
		int result =	db.doUpdate(sql, name,tel,prov,city,area,addr,ano);
		if( result > 0){
			this.send(resp, 1);
		}
	}
	private void delAddr(HttpServletRequest req, HttpServletResponse resp) {
		String ano =  req.getParameter("ano");
		String sql = "delete from addrinfo where ano = ?";
		int result = db.doUpdate(sql, ano);
		if( result > 0 ){
			this.send(resp, 1);
		}else{
			this.send(resp, 0);
		}
	}
	private void addAddr(HttpServletRequest req, HttpServletResponse resp) {
		Map<String,Object> user = (Map<String, Object>) req.getSession().getAttribute("loginUser");
		int mno = Integer.parseInt(user.get("mno") + "");
		
		String name = req.getParameter("name");
		String tel = req.getParameter("tel");
		String prov = req.getParameter("prov");
		String city = req.getParameter("city");
		String area = req.getParameter("area");
		String addr = req.getParameter("addr");
		
		String ano = new Date().getTime() + "";
		
		String sql = "insert into addrinfo values(?,?,?,?,?,?,?,?,0,1)";
		int result = db.doUpdate(sql, ano,mno,name,tel,prov,city,area,addr);
		if( result > 0 ){
			this.send(resp, ano);
		}else{
			this.send(resp, -1);
		}
		
	}
	private void findByMno(HttpServletRequest req, HttpServletResponse resp) {
		Map<String,Object> user = (Map<String, Object>) req.getSession().getAttribute("loginUser");
		int mno = Integer.parseInt(user.get("mno") + "");
		
		String sql = "select * from addrinfo where mno = ?";
		List<Map<String, Object>> list = db.findAll(sql, mno);
		
		if( list.size() > 0 ){
			this.send(resp, list);
		}else{
			this.send(resp, 0);
		}
		
	}

}
